<template>
  <div class="component-cp">
    <div class="table-page-search-wrapper">
      <a-form-model :model="queryParam" layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="所属顾问">
              <a-select
                show-search
                :filter-option="filterOption"
                v-model="queryParam['filter[adviser_uid]']"
                placeholder="选择所属顾问"
              >
                <a-select-option v-for="(item,index) in adviserOpts" :key="index" :value="index">{{item}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="业务编号">
              <a-input v-model="queryParam['filter[business_no]']" placeholder="输入业务编号" />
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="开启日期">
                <a-range-picker @change="onChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="合同编号">
                <a-input v-model="queryParam['filter[contract_no]']" placeholder="输入合同编号" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户来源">
                <a-select show-search v-model="queryParam['filter[source]']" :filter-option="filterOption" placeholder="选择客户来源">
                  <a-select-option v-for="(item,index) in sourceOpts" :key="index" :value="index">{{item}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="咨询类型">
                <a-select show-search v-model="queryParam['filter[first_type]']" :filter-option="filterOption" placeholder="选择咨询类型">
                  <a-select-option v-for="(item,index) in firstTypeOpts" :key="index" :value="index">{{item}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="联系电话">
                <a-input v-model="queryParam['filter[phone]']" placeholder="输入联系电话" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="有无尾款">
                <a-select
                  show-search
                  v-model="queryParam['filter[tail_money]']"
                  placeholder="选择是否含有尾款"
                >
                  <a-select-option value="yes">有尾款</a-select-option>
                  <a-select-option value="no">无尾款</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="!advanced && 8 || 24" :sm="24">
                <span class='table-page-search-submitButtons' :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                  <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                  <a-button style="margin-left: 8px" @click="reset">重置</a-button>
                  <a @click="toggleAdvanced" style="margin-left: 8px">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'" />
                  </a>
                </span>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <div class="table-operations">
      <a-card>
        <div slot="title">
          <a-button-group>
            <a-button @click="getUnread(1)">全部已读未读</a-button>
            <a-button @click="getUnread(0)">未读数</a-button>
          </a-button-group>
        </div>
        <div slot="extra">
          <a-button-group>
            <a-button type="danger" ghost @click="handleFilter('all')">全部状态</a-button>
            <a-button type="danger" ghost @click="handleFilter('status2_1')">材料待提交</a-button>
          </a-button-group>
        </div>
        <s-table
          bordered
          ref="table"
          :row-key="(record) => record.cb_id"
          :columns="cpColumns"
          :data="copyrightData"
        >
          <div
            slot="filterDropdown"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-input
              v-ant-ref="c => (searchInput = c)"
              :placeholder="`搜索${column.title}`"
              :value="selectedKeys[0]"
              style="width: 188px; margin-bottom: 8px; display: block;"
              @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
              @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterSelect"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              show-search
              style="width: 188px; margin-bottom: 8px; display: block;"
              :placeholder="`选择${column.title}`"
              :value="selectedKeys[0]"
              :filter-option="filterOption"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item,index) in adviserOpts" :key="index" :value="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <template slot="source" slot-scope="text">
            {{sourceOpts[text]}}
          </template>
          <template slot="first_type" slot-scope="text">
            {{firstTypeOpts[text]}}
          </template>
          <template slot="contract_no" slot-scope="text, record">
            {{text}}<br>
            {{record.business_no}}
          </template>
          <template slot="price" slot-scope="text">
            <a-tag color="red">{{text}}</a-tag>
          </template>
          <template slot="official_fee" slot-scope="text">
            <a-tag color="red">{{text}}</a-tag>
          </template>
          <template slot="agency_fee" slot-scope="text">
            <a-tag color="red">{{text}}</a-tag>
          </template>
          <template slot="status2" slot-scope="text, record">
            <a-tag color="red">{{record.status2_name}}</a-tag>
          </template>
          <template slot="action" slot-scope="text, record">
            <a-button type="primary" size="small" @click="toDetail(record.cb_id)">查看</a-button>
          </template>
        </s-table>
      </a-card>
    </div>
  </div>
</template>

<script>
import { STable } from '@/components'
import businessColumns from '@/columns/business'
import { getList } from '@/api/business'
const cpColumns = businessColumns.cpColumns

export default {
  props: {
    category_id: {
      type: String,
      default: '30'
    }
  },
  components: {
    STable
  },
  name: 'Copyright',
  data () {
    return {
      cpColumns,
      copyrightData: parameters => {
        return getList(Object.assign(parameters, this.queryParam)).then(res => {
          this.adviserOpts = res.result.adviser
          this.categoryOpts = res.result.sb_category_opts
          this.sourceOpts = res.result.source_opts
          this.firstTypeOpts = res.result.first_type_opts
          this.businessOpts = res.result.businesses
          return res.result
        })
      },
      advanced: true,
      queryParam: {},
      adviserOpts: {},
      categoryOpts: {},
      sourceOpts: {},
      firstTypeOpts: {},
      businessOpts: {}
    }
  },
  activated () {
    this.$refs.table.refresh()
  },
  created () {
    this.queryParam['filter[category]'] = this.category_id
  },
  methods: {
    onChange (date, dateString) {
      this.queryParam['filter[open_time_start]'] = dateString[0]
      this.queryParam['filter[open_time_end]'] = dateString[1]
    },
    handleFilter (event) {
      if (event !== 'all') {
        this.queryParam['filter[status2_state]'] = event
      } else {
        delete this.queryParam['filter[status2_state]']
      }
      this.$refs.table.refresh(true)
    },
    handleReset (clearFilters, dataIndex) {
      clearFilters()
      delete this.queryParam['filter[' + dataIndex + ']']
    },
    toDetail (id) {
      this.$router.push({ name: 'BusinessDetail', params: { id } })
    },
    onSubmitTime (date, dateString) {
      for (let i = 0; i < dateString.length; i++) {
        if (i === 0) {
          this.queryParam['filter[open_time_start]'] = dateString[i]
        }
        if (i === 1) {
          this.queryParam['filter[open_time_end]'] = dateString[i]
        }
      }
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    getUnread (read) {
      if (read === 0) {
        this.queryParam['filter[is_read]'] = read
      } else {
        delete this.queryParam['filter[is_read]']
      }
      this.$refs.table.refresh(true)
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    handleSearch (selectedKeys, confirm, dataIndex) {
      confirm()
      this.searchText = selectedKeys[0]
      this.searchedColumn = dataIndex
      this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
    },
    reset () {
      this.queryParam = {}
      this.queryParam['filter[category]'] = '30'
      this.$refs.table.refresh(true)
    }
  }
}
</script>

<style scoped>

</style>
